
$.wait = function(time) {
  return $.Deferred(function(dfd) {
    setTimeout(dfd.resolve, time);
  });
}

var levels=[],lev_label=[];

function getDataInfoStreams(c_id) {
	
    ajaxLoadingOn();
    
    $.post('infostreams/tree', {id:c_id},
      function(data){
        //alert("Data Loaded: " + data.message);
        //console.log(data);        
          
        
                
        bDatan = data.message;
        
        
        var w = $('.testmain_clouds').width();
    h = $('.testmain_clouds').height();
    
    var z = 20,
    x = w / z,
    y = h / z;
    
    

var radius = d3.scale.sqrt().range([0, 6]);

$('.testmain_clouds svg').remove();

var svg = d3.select(".testmain_clouds").append("svg")
    .attr("width", w)
    .attr("height", h);
    
        var nodes = [],links = [];
        
        var with_link=0;
        var node_id=0;
        var kk=0;
        var kk1=0;
        var kk2=0;
        var node_link=[];
        var node_link_p=[];
        var node_link_w=[];
             
        for(var i in bDatan){
            var c_s=0;
            if (bDatan[i]['parent']==0) {c_s=20;} else {c_s=15;}  
            nodes.push({"atom": bDatan[i]['title'],"size": c_s,"id_node":bDatan[i]['id'],"level_node":bDatan[i]['level'],"parent_node":bDatan[i]['parent'],"last_level":bDatan[i]['_is_last_level'],"n_id":with_link});
        
        node_id++;
        node_link[i]=bDatan[i]['id'];
        node_link_p[i]=bDatan[i]['parent'];
        node_link_w[i]=node_id-1;
        
        var data_child=bDatan[i]['_childs'];

            for(var k in data_child){
                
                var c_s1=c_s-6;            
                
                nodes.push({"atom": data_child[k]['title'],"size": c_s1,"id_node":data_child[k]['id'],"level_node":data_child[k]['level'],"parent_node":data_child[k]['parent'],"last_level":data_child[k]['_is_last_level'],"n_id":with_link});
                node_id++;
                node_link[k]=data_child[k]['id'];
                node_link_p[k]=data_child[k]['parent'];
                node_link_w[k]=node_id-1;
                
                //---------------------------------------------------------------------------
            var data_child1=data_child[k]['_childs']; 
                for(var k1 in data_child1){
                    var c_s2=c_s1-2;
                 
                 nodes.push({"atom": data_child1[k1]['title'],"size": c_s2,"id_node":data_child1[k1]['id'],"level_node":data_child1[k1]['level'],"parent_node":data_child1[k1]['parent'],"last_level":data_child1[k1]['_is_last_level'],"n_id":with_link});   
                    
                    node_id++;
                node_link[k1]=data_child1[k1]['id'];
                node_link_p[k1]=data_child1[k1]['parent'];
                node_link_w[k1]=node_id-1;
                    
                    var data_child2=data_child1[k1]['_childs'];
                    for(var k2 in data_child2){
                        var c_s3=c_s2-2;
                        nodes.push({"atom": data_child2[k2]['title'],"size": c_s3,"id_node":data_child2[k2]['id'],"level_node":data_child2[k2]['level'],"parent_node":data_child2[k2]['parent'],"last_level":data_child2[k2]['_is_last_level'],"n_id":with_link});   
                    
                        node_id++;
                        node_link[k2]=data_child2[k2]['id'];
                        node_link_p[k2]=data_child2[k2]['parent'];
                        node_link_w[k2]=node_id-1;
                        
                        //--------------------------
                         var data_child3=data_child2[k2]['_childs'];
                         for(var k3 in data_child3){
                            var c_s4=c_s3-2;
                        nodes.push({"atom": data_child3[k3]['title'],"size": c_s4,"id_node":data_child3[k3]['id'],"level_node":data_child3[k3]['level'],"parent_node":data_child3[k3]['parent'],"last_level":data_child3[k3]['_is_last_level'],"n_id":with_link});   
                    
                        node_id++;
                        node_link[k3]=data_child3[k3]['id'];
                        node_link_p[k3]=data_child3[k3]['parent'];
                        node_link_w[k3]=node_id-1;
                        
                            //-------------------
                            var data_child4=data_child3[k3]['_childs'];
                            for(var k4 in data_child4){
                              var c_s5=c_s4-1;
                        nodes.push({"atom": data_child4[k4]['title'],"size": c_s5,"id_node":data_child4[k4]['id'],"level_node":data_child4[k4]['level'],"parent_node":data_child4[k4]['parent'],"last_level":data_child4[k4]['_is_last_level'],"n_id":with_link});   
                    
                        node_id++;
                        node_link[k4]=data_child4[k4]['id'];
                        node_link_p[k4]=data_child4[k4]['parent'];
                        node_link_w[k4]=node_id-1;  
                                }
                            //-------------------
                            }
                        //--------------------------
                        
                        }
                    
                    }
                //---------------------------------------------------------------------------
                
           } //end for level 1
              with_link=with_link+kk+kk1+1+kk2;
              kk=0;
              kk1=0;
              kk2=0;
                   
        } //end for main level
        
        for (var i = 0; i < node_link.length; i++) {
            
            if (parseInt(node_link_p[i])>0) {
                
                for (var ii = 0; ii < node_link.length; ii++) {
                    if (parseInt(node_link[ii])==parseInt(node_link_p[i]))
                        {
                        var n_source=node_link_w[ii];
                        var n_target=node_link_w[i];  
                        links.push({"source": n_source, "target": n_target,  "bond": 1}); 
                        var kl=1;                         
                                 
                        }
                    }
                
               //links.push({"source": with_link+kk, "target": with_link+kk+kk1,  "bond": 1}); 
            }
            
            }
        var tt_n=node_link;
                
        //links.push({"source": 0, "target": 1,  "bond": 1});
        //links.push({"source": 0, "target": 2,  "bond": 1});
        //links.push({"source": 0, "target": 3,  "bond": 1});
        
        
        var force = d3.layout.force()
    .nodes(nodes)
    //.gravity(0.1)
    //.distance(180)
    .size([w, h])
    //.theta(1)
    .charge(-1000)
    //.friction(0.1)
    //.linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 40; });
    .linkDistance(90);
    
    force
      .nodes(nodes)
      .links(links)
      .size([w, h])
      .on("tick", tick)
      .start();
      
      var link = svg.selectAll(".link")
      .data(links)
    .enter().append("g")
      .attr("class", "link");

  link.append("line")
      .style("stroke-width", function(d) { return (d.bond * 2 - 1) * 2 + "px"; });

  link.filter(function(d) { return d.bond > 1; }).append("line")
      .attr("class", "separator");
      
      var node = svg.selectAll(".node")
      .data(nodes)
    .enter().append("g")
      .attr("class", function(d) { return "node n_id"+d.id_node })
      .attr("data-id", function(d) { return d.id_node })
      .attr("level_node", function(d) { return d.level_node })
      .attr("parent_node", function(d) { return d.parent_node })
      .attr("last_level", function(d) { return d.last_level })
      .attr("node-id", function(d,i) { return i })
      .attr("n_id", function(d) { return d.n_id })
      .attr("dx", function(d) { return d.x; })
     .attr("dy", function(d) { return d.y; })
      .call(force.drag)
      .on("mousedown", function(d) {
     d.fixed = true;
     d3.select(this).classed("sticky", true);
   });
   
   
   
    

   node.append("circle")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_none ifselect_triangle';
        } else {
            return 'ifselect';
        }
    })
      .attr("r", 38)
      .style("fill", "#000")
      .style("stroke-width", "5px")
      .style("stroke", "#fff");

var fill = d3.scale.linear()
    .domain([0, 9])
    .range(["brown", "steelblue"]);
    
    node.append("circle")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_none';
        } else {
            return 'main_circle blue1';
        }
    })
      .attr("r", function(d) { return radius(d.size)+3; })
      .style("fill", fill)
      .style("stroke", fill);
      
      
      node.append("image")
//    .attr('class',function(d) {
//        if (d.id_node==29) {
  //      return 'main_image_active';       
    //    } else {
      //  return 'main_image_noactive';    
//        }
  //  })
    .attr("xlink:href", "http://cytellect.net/tmp/node.png")
    .attr("width", "80px")
    .attr("height", "80px")
    .attr("y", "-40px")
      .attr("x", "-40px")
      .on("click", function(d) {
        var yy=this;
        node_click(d,yy);
        });
      
  node.append("circle")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_none';
        } else {
            return 'main_circle small_active';
        }
    })
      .attr("r", 10)
      .attr("cy", "-3em")
      .attr("cx", "2.4em")
      .style("fill", "#fff")
      .on("click",function(d) {
        
        kk=$(this).parent().attr('class').split(' ');
        
        var new_val=$('.node.get_select').find('text').eq(2).text();
    $('input.cloud_name').val(new_val);
        
        $('#myModalnew_edit').modal('show');
        
      });
      
      node.append("text")
      .attr('class','info_ico')
      .attr("dy", "-2.7em")
      .attr("dx", "2.4em")
      .attr("text-anchor", "left")
      .style("fill", "#000")
      .text(function(d) { return "i"; })
      .on("click",function(d) {
        
        kk=$(this).parent().attr('class').split(' ');
        
        var new_val=$('.node.get_select').find('text').eq(2).text();
    $('input.cloud_name').val(new_val);
        
        $('#myModalnew_edit').modal('show');
        
      });

    

    function rightRoundedRect(x, y, width, height, radius) {
  return "M" + x + "," + y
       + "h" + (width - radius)
       + "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
       + "v" + (height - 2 * radius)
       + "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
       + "h" + (radius - width)
       + "z";
}


function node_click(d,yy)
{
  if ($('.testmain_edit .btn i').hasClass('ico_white')) {
        
        kk=$(yy).parent().attr('class').split(' ');
        
        var tt=d3.select('.'+kk[1]).attr('class').split(' ');
        
        if ((tt[2]=='get_select')||(tt[1]=='get_select')||(tt[3]=='get_select'))
        {
        d3.select('.'+kk[1]).classed("get_select", false);
        //$('.sel_val').val('0');    
        $('.nested_val').val("true");
        d3.selectAll('.small_active').classed("small_select", false);
        d3.selectAll('.info_ico').classed("ico_active", false);
        
        } else {
        d3.selectAll(".node").classed("get_select", false);
        d3.select('.'+kk[1]).classed("get_select", true);
        $('.nested_val').val("false");
        
        $('.sel_val').val(d3.select('.'+kk[1]).attr('data-id'));
        d3.selectAll('.small_active').classed("small_select", false);
        d3.selectAll('.info_ico').classed("ico_active", false);
        
        d3.select('.'+kk[1]).select('.info_ico').classed("ico_active", true);
        d3.select('.'+kk[1]).select('.small_active').classed("small_select", true);
        
        }
        
        
    } else {
        
        kk=$(yy).parent().attr('class').split(' ');
        
        var tt=d3.select('.'+kk[1]).attr('class').split(' ');
        
        if ((tt[2]=='get_select')||(tt[1]=='get_select')||(tt[3]=='get_select'))
        {
        d3.select('.'+kk[1]).classed("get_select", false);
        //$('.sel_val').val('0');    
        $('.nested_val').val('true');
        
        
        d3.selectAll('.small_active').classed("small_select", false);
        d3.selectAll('.info_ico').classed("ico_active", false);
        
        var c_id=d3.select('.'+kk[1]).attr('data-id');
        var p_id=d3.select('.'+kk[1]).attr('parent_node');
        var l_n=d3.select('.'+kk[1]).attr('last_level');
                
        var node_title=d3.select('.'+kk[1]).select('text').text();
        $('.testmain_back.no_active').attr('data-id',p_id);
               
        var c_lev=levels.length;
        levels[c_lev]=parseInt(c_id);
        lev_label[c_lev]=node_title;
        
        getDataInfoStreams(c_id);
        
        } else {
        d3.selectAll(".node").classed("get_select", false);
        d3.select('.'+kk[1]).classed("get_select", true);
        $('.nested_val').val("false");
        
        $('.sel_val').val(d3.select('.'+kk[1]).attr('data-id'));
        d3.selectAll('.small_active').classed("small_select", false);
        d3.selectAll('.info_ico').classed("ico_active", false);
        
        d3.select('.'+kk[1]).select('.info_ico').classed("ico_active", true);
        d3.select('.'+kk[1]).select('.small_active').classed("small_select", true);
        }
        
        
        
                
        $('.breadcumps').html('');
        var new_bread='';
        
        for(ci = 1; ci <= c_lev; ci++){
            new_bread=new_bread+' -> '+lev_label[ci];
            }
            
        //$('.breadcumps').html(new_bread);
        
        
    }  
}
  
    node.append("path")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_line';
        } else {
            return 'main_circle_none';
        }
    })
    .attr("d",rightRoundedRect(-30, -20, 40, 40, 20))
    .attr("fill","#fff");

    node.append("circle")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_red';
        } else {
            return 'main_circle_none';
        }
    })
      .attr("r", 8)
      .attr("cy", "-2em")
      .attr("cx", "1.2em")
      .style("fill", "red")
      .on("click", function(d) {
        kk=$(this).parent().attr('class').split(' ');
        
        var tt=d3.select('.'+kk[1]).attr('class').split(' ');
        var c_id=d3.select('.'+kk[1]).attr('data-id');
        
        var node_title=d3.select('.'+kk[1]).select('text').text();
        
        var c_lev=levels.length;
        levels[c_lev]=parseInt(c_id);
        lev_label[c_lev]=node_title;
        $('.sel_val').val(d3.select('.'+kk[1]).attr('data-id'));
        $('.nested_val').val("true");
        getDataInfoStreams(c_id);
        })

  node.append("path")
  .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_none';
        } else {
            return 'main_image_active';
        }
    })
  .attr("d", d3.svg.arc()
    .innerRadius(function(d) {
        val_text=12;
        if ((d.id_node==29)||(d.id_node==140)) {val_text=12;}
        if ((d.id_node==85)||(d.id_node==68)) {val_text=12;}
        if (d.id_node==67) {val_text=12;}
        return val_text;
        })
    .outerRadius(function(d) { return 25; })
    .startAngle(function(d) {return 0;})
    .endAngle(function(d) {
        val_text=6;
        if ((d.id_node==29)||(d.id_node==140)) {val_text=4.5;}
        if ((d.id_node==85)||(d.id_node==68)) {val_text=5.3;}
        if (d.id_node==67) {val_text=4.1;}
        return val_text;
        }))
    .attr("fill", function(d) {
        if ((d.id_node==29)||(d.id_node==140)||(d.id_node==85)||(d.id_node==68)||(d.id_node==67)) {
            return "#fff";
            } else {
            return "#000";    
            }
    })
    .on("click", function(d) {
    //in edit mode
    
    var yy=this;
        node_click(d,yy);
    
     
   });
  
  node.append("circle")
    .attr('class',function(d){
        if (d.id_node==212) {
            return 'main_circle_none ifdata_triangle';
        } else {
            return 'ifdata';
        }
    })
      .attr("r", 8)
      .style("stroke", "#fff");
  
  node.append("text")
  .attr('class',function(d) {
        if ((d.id_node==29)||(d.id_node==140)||(d.id_node==85)||(d.id_node==68)||(d.id_node==67)) {
        return 'main_image_active percent_node';       
        } else {
        return 'main_image_noactive';    
        }
    })
      .attr("dy", function(d){
        val_text="2.2em";
        
        return val_text;
      })
      .attr("dx", "-0.8em")
      .attr("text-anchor", "left")
      .text(function(d) {
        if ((d.id_node==29)||(d.id_node==140)) {val_text='75%';}
        if ((d.id_node==85)||(d.id_node==68)) {val_text='85%';}
        if (d.id_node==67) {val_text='65%';}
        return val_text;
      });
      
      node.append("rect")
      .attr("width", 30)
    .attr("height", 13)
    .attr("x", 0)
    .attr("y", -6)
    .style("fill", "#000");

  node.append("text")
      .attr("dy", ".35em")
      .attr("text-anchor", "left")
      .text(function(d) { return d.atom; });
      
      function tick(d) {
    
    
    //d.fixed = true;
     
    
    node.attr("transform", function(d) {return "translate(" + d.x + "," + d.y + ")";});
    
    link.selectAll("line")
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
        
    
    
  }
        $('g.node').each(function(){
            var ifsel=$(this).attr('last_level');
            if (ifsel=="false") {
                d3.select(this).select('.ifdata').classed("ifdata_act", true);
            } else {
                d3.select(this).select('.ifdata').classed("ifdata_act", false);
                $(this).append('<div class="info_node">Детальная информация</div>');
            }
        });
        
        ajaxLoadingOff(); 
      }
           
    , "json");
      
};


 
////////////////////////////////////////////////////////////////////////////////////////
//functions called during ajax request to block user actions while data is being loaded:
function ajaxLoadingOn(){
		var remove;
        $('body').addClass('ajaxLoading');
        $('.container_ajax_loading').append('<div id="loading-page" class="loading"><div id="holder"></div><div id="loading-page-inner"></div></div>');
            remove = spinner("holder", 15, 40, 14, 6, "#fff");
        };

	
function ajaxLoadingOff(){
		$('body').removeClass('ajaxLoading');
        $('.container_ajax_loading').html('');  
	};
	
function isAjaxLoading(){
		if($('body').hasClass('ajaxLoading')) {
			return true;
		} else {
			return false;
		}
	};
////////////////////////////////////////////////////////////////////////////////////////
         


////////////////////////////////////////////////////////////////////////////////////////
// preloader
function get_number_f(kk) {
    var ll=kk+'';
    var ll2=ll[0];
    var ll1=ll.length;
    var new_v=ll;
    if (ll1==4) {
        new_v=ll[0]+' '+ll[1]+ll[2]+ll[3];
    }
    if (ll1==5) {
        new_v=ll[0]+ll[1]+' '+ll[2]+ll[3]+ll[4];
    }
    if (ll1==6) {
        new_v=ll[0]+ll[1]+ll[2]+' '+ll[3]+ll[4]+ll[5];
    }
    if (ll1==7) {
        new_v=ll[0]+' '+ll[1]+ll[2]+ll[3]+' '+ll[4]+ll[5]+ll[6];
    }
    if (ll1==8) {
        new_v=ll[0]+ll[1]+' '+ll[2]+ll[3]+ll[4]+' '+ll[5]+ll[6]+ll[7];
    }
    if (ll1==9) {
        new_v=ll[0]+ll[1]+ll[2]+' '+ll[3]+ll[4]+ll[5]+' '+ll[6]+ll[7]+ll[8];
    }
    //ll.formatNumber({format:"# ###", locale:"fr"});

    return new_v;
    
}

function spinner(holderid, R1, R2, count, stroke_width, colour) {
    var sectorsCount = count || 12,
        color = colour || "#fff",
        width = stroke_width || 15,
        r1 = Math.min(R1, R2) || 35,
        r2 = Math.max(R1, R2) || 60,
        cx = r2 + width,
        cy = r2 + width,
        r = Raphael(holderid, r2 * 2 + width * 2, r2 * 2 + width * 2),
        
        sectors = [],
        opacity = [],
        beta = 2 * Math.PI / sectorsCount,

        pathParams = {stroke: color, "stroke-width": width, "stroke-linecap": "round"};
        Raphael.getColor.reset();
    for (var i = 0; i < sectorsCount; i++) {
        var alpha = beta * i - Math.PI / 2,
            cos = Math.cos(alpha),
            sin = Math.sin(alpha);
        opacity[i] = 1 / sectorsCount * i;
        sectors[i] = r.path([["M", cx + r1 * cos, cy + r1 * sin], ["L", cx + r2 * cos, cy + r2 * sin]]).attr(pathParams);
        if (color == "rainbow") {
            sectors[i].attr("stroke", Raphael.getColor());
        }
    }
    var tick;
    (function ticker() {
        opacity.unshift(opacity.pop());
        for (var i = 0; i < sectorsCount; i++) {
            sectors[i].attr("opacity", opacity[i]);
        }
        r.safari();
        tick = setTimeout(ticker, 1000 / sectorsCount);
    })();
    return function () {
        clearTimeout(tick);
        r.remove();
    };
}

function set_disable()
{
    
}

var bDatan=[];

$(document).ready(function()
{
    d3.select("body").style("background-color", "black");
    $('.nested_val').val("false");
    levels[0]=0;
    
        var c_id='';
      getDataInfoStreams(c_id);
      
      
  // setInterval('getDataInfoStreams()', 30000);
  
  if (document.getElementById('main_info')!=null) {
    $('.container').addClass('main_info_page');
    $('.container div.row').eq(1).addClass('main_info_row');
  }

    $('.testmain_edit a').click(function(){
        if ($('.testmain_add').hasClass('view')) {
        $('.testmain_add').removeClass('view');
        $('.testmain_remove').removeClass('view');
        $('.testmain_edit_node').removeClass('view');
        
        d3.selectAll(".node").classed("get_select", false);
        d3.selectAll('.small_active').classed("small_select", false);
        $('.sel_val').val('0');
        $('.nested_val').val("false");
        $('.testmain_edit a .icon-edit').attr('class','icon-edit');
        
        set_disable();
        
    } else {
        $('.nested_val').val("false");
        $('.testmain_add').addClass('view');
        $('.testmain_remove').addClass('view');
        $('.testmain_edit_node').addClass('view');
        $('.testmain_edit a .icon-edit').attr('class','icon-edit ico_white');
    }
        
       return false; 
    });
    
    $('.testmain_back.no_active a').click(function(){
                
        levels.pop();
        lev_label.pop();
        var lev_c=levels.length;
        var p_id=levels[lev_c-1];
        
        $('.breadcumps').html('');
        var new_bread='';
        
        for(ci = 1; ci <= lev_c-1; ci++){
            new_bread=new_bread+' -> '+lev_label[ci];
            }
            
        $('.breadcumps').html(new_bread);
        
        getDataInfoStreams(p_id);
        
       return false; 
    });
    
       
    $('.testmain_edit').click(function(){
        return false;
    });
    
    $('.testmain_remove').click(function(){

    });
  
  $('.testmain_edit_node').click(function(){
    
    $('#myModalnew .modal-footer .btn').eq(1).removeClass('add_new');
    $('#myModalnew .modal-footer .btn').eq(1).addClass('edit_new');
    
    var new_val=$('.node.get_select').find('text').eq(2).text();
    $('input.cloud_name').val(new_val);
    $('.edit_head').text('Редактировать облако');
    });
  
  $('.testmain_add').click(function(){
    $('#myModalnew .modal-footer .btn').eq(1).removeClass('edit_new');
    $('#myModalnew .modal-footer .btn').eq(1).addClass('add_new');
    $('.edit_head').text('Добавить облако');
    });
  
  $('#myModalnew .modal-footer .btn').eq(1).click(function(){
    if ($(this).hasClass('add_new')) {
        var cloud_name=$('input.cloud_name').val();
        if (cloud_name!="") {
        $('#myModalnew').modal('hide');
      ajaxLoadingOn();
      
      var c_id=0;

      
           
      var t=$('.sel_val').val();
      if (parseInt(t)==0) {
        c_id=0;
        } 
        else {
        c_id=t;
      }
      
      var nested_level=$('.nested_val').val();
      
      $.post('infostreams/add', {"parent":c_id,"nested_level":nested_level,"title":cloud_name},
      function(data){
        }
        , "json");
        
      $.wait(1000).then(function(){
        
        var c_lev=levels.length;
        var cur_level=levels[c_lev-1];
        
        getDataInfoStreams(cur_level);
         
    
        ajaxLoadingOff();
        });
        }
    }
    
    if ($(this).hasClass('edit_new')) {
        var cloud_name=$('input.cloud_name').val();
        $('.node.get_select').find('text').text(cloud_name);
        $('#myModalnew').modal('hide');
        }
        
  });  
  
  $('#myModalnew_del .btn.btn-primary-del').click(function(){ //del node
    
    $('#myModalnew_del').modal('hide');
      ajaxLoadingOn();
      
    var t=$('.node.get_select').attr('data-id');
    
    $.post('infostreams/delete', {"id":t},
      function(data){
        }
        , "json");
        
    $.wait(1000).then(function(){
        
        var c_lev=levels.length;
        var cur_level=levels[c_lev-1];
        
        getDataInfoStreams(cur_level);
        
        ajaxLoadingOff();
        });
  });
    
function update_add(t) {
    
    
    var w = $('.testmain_clouds').width()-100;
    h = $('.testmain_clouds').height()-100;
    
    var color = d3.scale.category20();

var radius = d3.scale.sqrt().range([0, 6]);

var t1=$('.node').last().attr('node-id');

var t2=parseInt(t1)+1;

$('.testmain_clouds svg').remove();

var svg = d3.select(".testmain_clouds").append("svg")
    .attr("width", w)
    .attr("height", h);

var cloud_name=$('input.cloud_name').val();
var if_select='';


nodes.push({"atom": cloud_name, "size": 15,"id_node":"new", "level_node":"2"});
if (parseInt(t)>=0) {
  links.push({"source": parseInt(t), "target": t2,  "bond": 1});
}



var force = d3.layout.force()
    .nodes(nodes)
    //.gravity(0.1)
    //.distance(180)
    .size([w, h])
    //.theta(1)
    .charge(-900)
    //.friction(0.1)
    //.linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 40; });
    .linkDistance(60);
    
    force
      .nodes(nodes)
      .links(links)
      .size([w, h])
      .on("tick", tick)
      .start();
      
      var link = svg.selectAll(".link")
      .data(links)
    .enter().append("g")
      .attr("class", "link");

  link.append("line")
      .style("stroke-width", function(d) { return (d.bond * 2 - 1) * 2 + "px"; });

  link.filter(function(d) { return d.bond > 1; }).append("line")
      .attr("class", "separator");
      
      var node = svg.selectAll(".node")
      .data(nodes)
    .enter().append("g")
      .attr("class", "node")
      .attr("data-id", function(d) { return d.id_node })
      .attr("level_node", function(d) { return d.level_node })
      .attr("parent_node", function(d) { return d.parent_node })
      .attr("last_level", function(d) { return d.last_level })
      .attr("node-id", function(d,i) { return i })
      .attr("cx", function(d) { return d.x; })
     .attr("cy", function(d) { return d.y; })
      .call(force.drag)
      .on("mousedown", function(d) {
     d.fixed = true;
     d3.select(this).classed("sticky", true);
   })
   .on("click", function(d) {
    //in edit mode
    
    if ($('.testmain_edit .btn i').hasClass('ico_white')) {
        
        var tt=d3.select(this).attr('class').split(' ');
        
        if ((tt[2]=='get_select')||(tt[1]=='get_select'))
        {
        d3.select(this).classed("get_select", false);    
        } else {
        d3.selectAll(".node").classed("get_select", false);
        d3.select(this).classed("get_select", true);
        }
        
        
    } else {
     d.fixed = false;
     d3.select(this).classed("sticky", false);   
    }
    
     
   });
   
   node.append("circle")
    .attr('class','ifselect')
      .attr("r", function(d) { var n_rad=radius(d.size)+10; return n_rad; })
      .style("fill", "#33475d");

  node.append("circle")
    .attr('class','main_circle')
      .attr("r", function(d) { return radius(d.size); })
      .style("fill", function(d) { return color(d.atom); });

  node.append("text")
      .attr("dy", ".35em")
      .attr("text-anchor", "left")
      .text(function(d) { return d.atom; });
      
      function tick(d) {
    
    
    //d.fixed = true;
     
    
    node.attr("transform", function(d) {return "translate(" + d.x + "," + d.y + ")";});
    
    link.selectAll("line")
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
        
    
    
  }

}    

//svg.selectAll("rect")
//    .data(d3.range(x * y))
//  .enter().append("rect")
//    .attr("transform", translate)
//    .attr("width", z)
//    .attr("height", z)
//    .style("fill", function(d) { return d3.hsl(d % x / x * 360, 1, Math.floor(d / x) / y); })
//    .on("mouseover", mouseover);
//
//function translate(d) {
//  return "translate(" + (d % x) * z + "," + Math.floor(d / x) * z + ")";
//}
//
//function mouseover(d) {
//  this.parentNode.appendChild(this);
//
//  d3.select(this)
//      .style("pointer-events", "none")
//    .transition()
//      .duration(1500)
//      .attr("transform", "translate(480,480)scale(23)rotate(180)")
//    .transition()
//      .delay(1500)
//      .attr("transform", "translate(240,240)scale(0)")
//      .style("fill-opacity", 0)
//      .remove();
//}
    
    if (document.getElementById('testmain_center')!=null) {
    $('.main_info_head').hide();
    $('.navbar').hide();
    $('hr').hide();
    $('.new_f').hide();
    $('body').css('padding-top','0px');
  }
    
    
});



$(function() {
    
    var currentTime = new Date ( );
      var currentHours = currentTime.getHours ( );
      var currentMinutes = currentTime.getMinutes ( );
      var currentSeconds = currentTime.getSeconds ( );
      
      currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
      currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
      
      var currentYear=currentTime.getFullYear();
      var currentDay=currentTime.getDate();
      var currentMonth=currentTime.getMonth();
      currentMonth=currentMonth+1;
      
      currentDay = ( currentDay < 10 ? "0" : "" ) + currentDay;
      currentMonth = ( currentMonth < 10 ? "0" : "" ) + currentMonth;
      
      
      //date_start1=currentYear+'-'+currentMonth+'-'+currentDay+' '+'00:00:00';
      date_start1='2012-09-28 15:00:00';
      date_end1=currentYear+'-'+currentMonth+'-'+currentDay+' '+currentHours+':'+currentMinutes+':'+currentSeconds;
    
    //getDataInfoStreams(date_start1,date_end1);

   });

/////////////////////////////////////////////////////